home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / PickTest / PickTest.java.z / PickTest.java
Encoding:
Java Source  |  2003-08-08  |  13.1 KB  |  416 lines

  1. /*
  2.  *    @(#)PickTest.java 1.9 02/10/21 13:48:53
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import com.sun.j3d.utils.picking.behaviors.*;
  41. import com.sun.j3d.utils.picking.*;
  42.  
  43. import java.applet.Applet;
  44. import java.awt.BorderLayout;
  45. import java.awt.event.*;
  46. import java.awt.Component;
  47. import com.sun.j3d.utils.applet.MainFrame;
  48. import com.sun.j3d.utils.universe.*;
  49. import javax.media.j3d.*;
  50. import javax.vecmath.*;
  51. import java.awt.Point;
  52. import javax.swing.*;
  53. import javax.swing.border.BevelBorder;
  54.  
  55. /**
  56.  * PickTest shows how to use the Picking utilities on various GeometryArray
  57.  * subclasses and Morph object.
  58.  * Type of Geometry      : CompressedGeometry ( GullCG.java )
  59.  *                         IndexedQuadArray ( CubeIQA.java )
  60.  *                         TriangleArray ( TetrahedronTA.java )
  61.  *                         IndexedTriangleArray ( TetrahedronITA.java )
  62.  *                         TriangleFanArray ( OctahedronTFA.java )
  63.  *                         IndexedTriangleFanArray ( OctahedronITA.java )
  64.  *                         TriangleStripArray ( IcosahedronTFA.java )
  65.  *                         IndexedTriangleStripArray ( IcosahedronITA.java )
  66.  *               PointArray( TetrahedronPA.java )
  67.  *               LineArray( TetrahedronLA.java )
  68.  *               IndexLineArray( TetrahedronILA.java )
  69.  *               LineStripArray( TetrahedronLSA.java )
  70.  *               IndexLineStripArray( TetrahedronILSA.java )
  71.  *
  72.  * Morph Object uses :     QuadArray ( ColorCube.java, ColorPyramidDown.java, 
  73.  *                and ColorPyramidUp.java ).
  74.  */
  75.  
  76. public class PickTest extends Applet implements ActionListener {
  77.   
  78.   private View view = null;
  79.   private QuadArray geomMorph[] = new QuadArray[3];
  80.   private Morph morph;
  81.  
  82.   private PickRotateBehavior behavior1;
  83.   private PickZoomBehavior   behavior2;
  84.   private PickTranslateBehavior behavior3;
  85.  
  86.     private SimpleUniverse u = null;
  87.  
  88.   public BranchGroup createSceneGraph(Canvas3D canvas)
  89.   {
  90.     // Create the root of the branch graph
  91.     BranchGroup objRoot = new BranchGroup();
  92.  
  93.     // Create a Transformgroup to scale all objects so they
  94.     // appear in the scene.
  95.     TransformGroup objScale = new TransformGroup();
  96.     Transform3D t3d = new Transform3D();
  97.     t3d.setScale(1.0);
  98.     objScale.setTransform(t3d);
  99.     objRoot.addChild(objScale);
  100.     
  101.     // Create a bunch of objects with a behavior and add them
  102.     // into the scene graph.
  103.     
  104.     int row, col;
  105.     int numRows = 4, numCols = 4;
  106.     
  107.     for (int i = 0; i < numRows; i++) {
  108.       double ypos = (double)(i - numRows/2) * 0.45 + 0.25;
  109.       for (int j = 0; j < numCols; j++) {
  110.     double xpos = (double)(j - numCols/2) * 0.45 + 0.25;
  111.     objScale.addChild(createObject(i * numCols + j, 0.1,  xpos, ypos));
  112.       }
  113.     }
  114.  
  115.     BoundingSphere bounds =
  116.       new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  117.  
  118.     // Add a light.
  119.     Color3f lColor = new Color3f(1.0f, 1.0f, 1.0f) ;
  120.     Vector3f lDir  = new Vector3f(0.0f, 0.0f, -1.0f) ;
  121.  
  122.     DirectionalLight lgt = new DirectionalLight(lColor, lDir) ;
  123.     lgt.setInfluencingBounds(bounds) ;
  124.     objRoot.addChild(lgt) ;
  125.  
  126.  
  127.     // Now create the Alpha object that controls the speed of the
  128.     // morphing operation.
  129.     Alpha morphAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE |
  130.                  Alpha.DECREASING_ENABLE,
  131.                  0, 0,
  132.                  4000, 1000, 500,
  133.                  4000, 1000, 500);
  134.       
  135.     // Finally, create the morphing behavior
  136.     MorphingBehavior mBeh = new MorphingBehavior(morphAlpha, morph);  
  137.     mBeh.setSchedulingBounds(bounds);
  138.     objRoot.addChild(mBeh);
  139.         
  140.     behavior1 = new PickRotateBehavior(objRoot, canvas, bounds);
  141.     objRoot.addChild(behavior1);
  142.  
  143.     behavior2 = new PickZoomBehavior(objRoot, canvas, bounds);
  144.     objRoot.addChild(behavior2);
  145.  
  146.     behavior3 = new PickTranslateBehavior(objRoot, canvas, bounds);
  147.     objRoot.addChild(behavior3);
  148.  
  149.     // Let Java 3D perform optimizations on this scene graph.
  150.     objRoot.compile();
  151.  
  152.     return objRoot;
  153.   }
  154.   
  155.  
  156.   private Group createObject(int index, double scale, double xpos, double ypos){
  157.     
  158.     Shape3D shape = null;
  159.     Geometry geom = null;
  160.      
  161.     // Create a transform group node to scale and position the object.
  162.     Transform3D t = new Transform3D();
  163.     t.set(scale, new Vector3d(xpos, ypos, 0.0));
  164.     TransformGroup objTrans = new TransformGroup(t);
  165.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  166.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  167.     objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
  168.     
  169.     // Create a second transform group node and initialize it to the
  170.     // identity.  Enable the TRANSFORM_WRITE capability so that
  171.     // our behavior code can modify it at runtime.
  172.     TransformGroup spinTg = new TransformGroup();
  173.     spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  174.     spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  175.     spinTg.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
  176.  
  177.     Appearance appearance = new Appearance();
  178.  
  179.     switch(index) {
  180.     case 0:
  181.       geom = new GullCG();
  182.       break;
  183.     case 1:
  184.       geom = new TetrahedronTA();
  185.       break;
  186.     case 2:
  187.       geom = new OctahedronTFA();  
  188.       break;
  189.     case 3:
  190.       geom = new IcosahedronTSA();    
  191.       break;
  192.     case 4:
  193.       geom = new CubeIQA();   
  194.       break;
  195.     case 5:
  196.       geom = new TetrahedronITA();  
  197.       break;
  198.     case 6:
  199.       geom = new OctahedronITFA();  
  200.       break;
  201.     case 7:
  202.       geom = new IcosahedronITSA();  
  203.       break;
  204.     case 8:
  205.       geomMorph[0] = new ColorPyramidUp();
  206.       geomMorph[1] = new ColorCube();
  207.       geomMorph[2] = new ColorPyramidDown();
  208.       break;
  209.     case 9:
  210.       geom = new TetrahedronLA();
  211.       break;
  212.     case 10:
  213.       geom = new TetrahedronILA();
  214.       break;
  215.     case 11:
  216.       geom = new TetrahedronLSA();
  217.       break;
  218.     case 12:
  219.       geom = new TetrahedronILSA();
  220.       break;
  221.     case 13:
  222.       geom = new TetrahedronPA();
  223.       break;
  224.     case 14:
  225.       geom = new TetrahedronIPA();
  226.       break;
  227.     // TODO: other geo types, Text3D?
  228.     case 15:
  229.       geom = new TetrahedronTA();
  230.       break;
  231.     }
  232.  
  233.     Material m = new Material() ;
  234.         
  235.     if(index == 8) {
  236.     m.setLightingEnable(false) ;
  237.     appearance.setMaterial(m) ;
  238.     morph = new Morph((GeometryArray[]) geomMorph, appearance);
  239.     morph.setCapability(Morph.ALLOW_WEIGHTS_READ);
  240.     morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
  241.     PickTool.setCapabilities(morph, PickTool.INTERSECT_FULL);
  242.     spinTg.addChild(morph); 
  243.     } else {
  244.     // Geometry picking require this to be set.
  245.     if (index == 0)
  246.         m.setLightingEnable(true) ;
  247.     else
  248.         m.setLightingEnable(false) ;
  249.     appearance.setMaterial(m) ;
  250.  
  251.     if ((index == 13) || (index == 14)) {
  252.         PointAttributes pa = new PointAttributes();
  253.         pa.setPointSize(4.0f);
  254.         appearance.setPointAttributes(pa);
  255.     }
  256.  
  257.     shape = new Shape3D(geom,appearance);
  258.     shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
  259.     shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
  260.     shape.setCapability(Shape3D.ENABLE_PICK_REPORTING);
  261.     PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
  262.     spinTg.addChild(shape);
  263.     } 
  264.      
  265.     // add it to the scene graph. 
  266.     objTrans.addChild(spinTg);    
  267.  
  268.     return objTrans;
  269.   }
  270.  
  271.   private void setPickMode(int mode) {
  272.       behavior1.setMode(mode);
  273.       behavior2.setMode(mode);
  274.       behavior3.setMode(mode);
  275.   }
  276.  
  277.   private void setPickTolerance(float tolerance) {
  278.       behavior1.setTolerance(tolerance);
  279.       behavior2.setTolerance(tolerance);
  280.       behavior3.setTolerance(tolerance);
  281.   }
  282.  
  283.   private void setViewMode(int mode) {
  284.       view.setProjectionPolicy(mode);
  285.   }
  286.  
  287.   // GUI stuff
  288.  
  289.   String pickModeString = new String("Pick Mode");
  290.   String boundsString = new String("BOUNDS");
  291.   String geometryString = new String("GEOMETRY");
  292.   String geometryIntersectString = new String("GEOMETRY_INTERSECT_INFO");
  293.   String toleranceString = new String("Pick Tolerance");
  294.   String tolerance0String = new String("0");
  295.   String tolerance2String = new String("2");
  296.   String tolerance4String = new String("4");
  297.   String tolerance8String = new String("8");
  298.   String viewModeString = new String("View Mode");
  299.   String perspectiveString = new String("Perspective");
  300.   String parallelString = new String("Parallel");
  301.  
  302.   private void addRadioButton(JPanel panel, ButtonGroup bg, String ownerName,
  303.           String buttonName, boolean selected) {
  304.       JRadioButton    item;
  305.       item = new JRadioButton(buttonName);
  306.       item.setName(ownerName);
  307.       item.addActionListener(this);
  308.       if (selected) {
  309.       item.setSelected(true);
  310.       }
  311.       panel.add(item);
  312.       bg.add(item);
  313.   }
  314.  
  315.   private void setupGUI(JPanel panel) {
  316.       ButtonGroup     bg;
  317.  
  318.       panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  319.       panel.setBorder(new BevelBorder(BevelBorder.RAISED));
  320.  
  321.       panel.add(new JLabel(pickModeString));
  322.       bg = new ButtonGroup();
  323.       addRadioButton(panel, bg, pickModeString, boundsString, true);
  324.       addRadioButton(panel, bg, pickModeString, geometryString, false);
  325.       addRadioButton(panel, bg, pickModeString, geometryIntersectString, false);
  326.  
  327.       panel.add(new JLabel(toleranceString));
  328.       bg = new ButtonGroup();
  329.       addRadioButton(panel, bg, toleranceString, tolerance0String,false);
  330.       addRadioButton(panel, bg, toleranceString, tolerance2String,true);
  331.       addRadioButton(panel, bg, toleranceString, tolerance4String,false);
  332.       addRadioButton(panel, bg, toleranceString, tolerance8String,false);
  333.  
  334.       panel.add(new JLabel(viewModeString));
  335.       bg = new ButtonGroup();
  336.       addRadioButton(panel, bg, viewModeString, perspectiveString, true);
  337.       addRadioButton(panel, bg, viewModeString, parallelString, false);
  338.  
  339.   }
  340.  
  341.   public void actionPerformed(ActionEvent e) {
  342.       String name = ((Component)e.getSource()).getName();
  343.       String value = e.getActionCommand();
  344.       //System.out.println("action: name = " + name + " value = " + value);
  345.       if (name == pickModeString) {
  346.      if (value == boundsString) {
  347.          setPickMode(PickCanvas.BOUNDS);
  348.      } else if (value == geometryString) {
  349.          setPickMode(PickCanvas.GEOMETRY);
  350.      } else if (value == geometryIntersectString) {
  351.          setPickMode(PickCanvas.GEOMETRY_INTERSECT_INFO);
  352.      } else {
  353.          System.out.println("Unknown pick mode: " + value); 
  354.      }
  355.       } else if (name == toleranceString) {
  356.      if (value == tolerance0String) {
  357.          setPickTolerance(0.0f);
  358.      } else if (value == tolerance2String) {
  359.          setPickTolerance(2.0f);
  360.      } else if (value == tolerance4String) {
  361.          setPickTolerance(4.0f);
  362.      } else if (value == tolerance8String) {
  363.          setPickTolerance(8.0f);
  364.      } else {
  365.          System.out.println("Unknown tolerance: " + value); 
  366.      }
  367.       } else if (name == viewModeString) {
  368.      if (value == perspectiveString) {
  369.          setViewMode(View.PERSPECTIVE_PROJECTION);
  370.      } else if (value == parallelString) {
  371.          setViewMode(View.PARALLEL_PROJECTION);
  372.      } 
  373.       } else {
  374.      System.out.println("Unknown action name: " + name); 
  375.       }
  376.   }
  377.   
  378.   public PickTest (){
  379.   }
  380.  
  381.     public void init() {
  382.     setLayout(new BorderLayout());
  383.     Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  384.     add("Center", c);
  385.     
  386.     JPanel guiPanel = new JPanel();
  387.     setupGUI(guiPanel);
  388.     add(guiPanel, BorderLayout.EAST);
  389.     
  390.     // Create a scene and attach it to the virtual universe
  391.     BranchGroup scene = createSceneGraph(c);
  392.     u = new SimpleUniverse(c);
  393.     
  394.     // This will move the ViewPlatform back a bit so the
  395.     // objects in the scene can be viewed.
  396.     u.getViewingPlatform().setNominalViewingTransform();
  397.     view = u.getViewer().getView();
  398.     
  399.     u.addBranchGraph(scene);
  400.     }
  401.  
  402.     public void destroy() {
  403.     u.cleanup();
  404.     }
  405.   
  406.   
  407.   public static void main(String argv[])
  408.   {
  409.     
  410.     BranchGroup group;
  411.     
  412.     new MainFrame(new PickTest(), 750, 550);
  413.   }
  414. }
  415.  
  416.